<?php

class single_edit_handler extends _site_handler {
	public $QueryExtra;
	public $BackLink = 'list';
	public $DatabaseUpdated = FALSE;
	public $NothingToUpdateError = FALSE;
	public $FormAction;
	public $Form;
	public $ErrorMessage;

	public function __construct($m) {
		$this->InitializeSite($m, $view);
		$db_info = $this->Lade();

		$section = @$_GET['section'];
		$orderby = @$_GET['order'];
		$ascdesc = @$_GET['ascdesc'];
		$revid = @$_GET['revid'];
		$revcols = @$_GET['revcols'];

		$section_info = $db_info->Sections[$section];
		$current_link = '';

		/* if (User->Rights[section] == NULL || !User->Rights[section]->Update) {
			return;
			// no access
		} */

		$pk = @$_GET['pk'];
		$name = @$_GET['name'];
		$value = @$_GET['value'];

		$subsection_info = NULL;

		if ( $section_info != NULL ) {
			$subsection = @$_GET['subsection'];
			$suborderby = @$_GET['suborder'];
			$subascdesc = @$_GET['subascdesc'];

			if ( $section_info->SubSection != NULL && $section_info->SubSection->Name == $subsection ) $subsection_info = $section_info->SubSection;

			$subpk = @$_GET['subpk'];

			$query_pk;

			$this->FormAction = $_SERVER['REQUEST_URI'];

			if ( $subsection_info != NULL ) {
				if ( @$_GET['backlink'] != NULL ) {
					$this->BackLink = @$_GET['backlink'];
				} else {
					$this->BackLink .= '?section=' . $section_info->Name . '&pk=' . $pk;
					if ( $orderby ) $this->BackLink .= '&order=' . $orderby . '&ascdesc=' . $ascdesc;
					$this->BackLink .= '&subsection=' . $subsection_info->Name;
					if ( $suborderby ) $this->BackLink .= '&suborder=' . $suborderby . '&subascdesc=' . $subascdesc;
				}

				$section_info->SetValue($pk, NULL);
				$subsection_info->SetValue($subpk, $_SERVER['REQUEST_METHOD'], $revid, $revcols);

				$query_pk = $subpk;

				$this->SectionInfo = $subsection_info;
				$this->SectionInfo->ParentSection = $section_info;
			} elseif ( $section_info != NULL ) {
				$this->BackLink .= '?section=' . $section_info->Name;
				if ( $orderby ) $this->BackLink .= '&order=' . $orderby . '&ascdesc=' . $ascdesc;

				$query_pk = $pk;

				$section_info->SetValue($pk, $_SERVER['REQUEST_METHOD'], $revid, $revcols);
				$this->SectionInfo = $section_info;
			}

			if ( $_SERVER['REQUEST_METHOD'] == 'POST' && $this->SectionInfo != NULL ) {
				$this->DatabaseUpdated = $this->SectionInfo->SaveValue();
			}

			$column = $section_info->Table->GetColumnBySimpleName($name);

			if ( $column->Editable && $column->AllowGet ) {
				$section_info->Value[$column->Name] = $value;
				$section_info->SaveValue();

				$this->Redirect($this->BackLink);
			} else {
				$this->ErrorMessage = 'Column not editable';
			}
		}

		$this->RenderViewContent($view);
	}
}
